package com.easyui.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easyui.bean.*;
import com.easyui.common.*;

public class GetOwnDiscussServlet extends HttpServlet {

	public GetOwnDiscussServlet() {}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		List<HeadNote> hnlist = null;
		List<Map> initList = null;
		JSONObject jsonAll = null;
		
		HttpSession session = request.getSession(); //获的session
		String role = (String)session.getAttribute("role");
		String userID = (String)session.getAttribute("userID");
		
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));//page和rows需要返回
		String orderBy = request.getParameter("order");//排序方式,不用返回
		String sortName = request.getParameter("sort");//排序字段,不用返回
		
		int pno = Integer.parseInt(request.getParameter("pno"));
System.out.println(pno);
		
		int totalRecords = 0;
		jsonAll= new JSONObject();//装所有信息，包括total参数
		SelNote sn = new SelNote();
		//Student st = ss.getStudent(sno, sname, sex, major, grade, stype);
		initList = sn.getOwnHeadNote(userID, pno, orderBy, sortName);
		hnlist = sn.getOwnSpeHeadNote(userID, initList, pno, page, rows, orderBy, sortName);
		
		if (hnlist != null && hnlist.size() > 0) {
			JSONArray jsonHeadNote = new JSONArray();//装学生信息
			for (int i = 0; i < hnlist.size(); i++) {
				HeadNote hn = (HeadNote)hnlist.get(i);
				int replyNum = 0;
				JSONObject j = new JSONObject();
				
				j.put("nno", hn.getNno());
				j.put("ntitle", hn.getNtitle());
				j.put("pubname", hn.getPubname());
				j.put("ptime", hn.getPtime());
				j.put("ltime", hn.getLtime());
				
				if(hn.getReplyList() == null) { 
					replyNum = 0;
				} else {
					replyNum = hn.getReplyList().size();
				}
				j.put("replyNum", replyNum);
				
				jsonHeadNote.add(j);
			}
			
			totalRecords = initList.size();
			jsonAll.put("total", totalRecords);
			jsonAll.put("rows", jsonHeadNote);
			
			//out.println(" { \"total\": " + totalRecords + ", \"rows\": " + json.toString() + "}");
		} else {
			totalRecords = 0;
			jsonAll.put("total", totalRecords);
			jsonAll.put("rows", "");
		}
		
		out.print(jsonAll);
		out.flush();
		out.close();
	}


}
